package com.ztstkj.lzservices.extension

import kotlin.math.abs
import kotlin.math.floor
import kotlin.math.pow

object IntExtension {
    fun Int.toHex4X(): String {
        return String.format("%04X", this).padStart(8,'0')
    }
    fun Int.toHex2X(): String {
        return String.format("%02X", this).padStart(4,'0')
    }
    fun Int.toHex1X(): String {
        return String.format("%01X", this).padStart(2,'0')
    }
    fun Int.dump(prefix: String? = "") {
        System.err.println("${prefix ?: "输出"}：$this")
    }

    fun Double.toChinese():String{
        var n1 = this
        val fraction = arrayOf("角", "分")
        val digit = arrayOf("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
        val unit = arrayOf(arrayOf("元", "万", "亿"), arrayOf("", "拾", "佰", "仟"))

        val head = if (n1 < 0) "负" else ""
        n1 = abs(n1)

        var s = ""
        for (i in fraction.indices) {
            s += (digit[(floor(n1 * 10.0 * 10.0.pow(i.toDouble())) % 10).toInt()] + fraction[i]).replace("(零.)+".toRegex(), "")
        }
        if (s.isEmpty()) {
            s = "整"
        }
        var integerPart = floor(n1).toInt()

        var i = 0
        while (i < unit[0].size && integerPart > 0) {
            var p = ""
            var j = 0
            while (j < unit[1].size && n1 > 0) {
                p = digit[integerPart % 10] + unit[1][j] + p
                integerPart /= 10
                j++
            }
            s = p.replace("(零.)*零$".toRegex(), "").replace("^$".toRegex(), "零") + unit[0][i] + s
            i++
        }
        var resut= head + s.replace("(零.)*零元".toRegex(), "元").replaceFirst("(零.)+".toRegex(), "").replace(
            "(零.)+".toRegex(),
            "零"
        ).replace(
            "^整$".toRegex(), "零元整"
        )
        resut=resut.replace("元","").replace("整","")
        return resut
    }
    fun Int.toChinese2(): String{
        var num = this
        var reversed = 0L
        //将输入的数字进行翻转
        while (num != 0) {
            val digit = num % 10
            reversed = reversed * 10 + digit
            num /= 10
        }
        val str = reversed.toString().toCharArray()
        val arr = arrayOf('园', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰', '仟')
        var outNum = ""
        //小写替换成大写
        for ((ind, e) in str.withIndex()) {
            var temp = ""
            when (e) {
                '0' -> temp = "零"
                '1' -> temp = "壹"
                '2' -> temp = "贰"
                '3' -> temp = "叁"
                '4' -> temp = "肆"
                '5' -> temp = "伍"
                '6' -> temp = "陆"
                '7' -> temp = "柒"
                '8' -> temp = "捌"
                '9' -> temp = "玖"
            }
            outNum = temp + arr[ind + 1] + outNum
        }

        var result = outNum
        for (i in str.indices) {
            result = result.replace("零拾", "零")
                .replace("零佰", "零")
                .replace("零仟", "零")
                .replace("零万", "万")
                .replace("零亿", "亿")
                .replace("零零", "零")
                .replace("零元", "元")
                .replace("元零", "元")
                .replace("亿万", "亿")
        }

        return result
    }
}